home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11135 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Fun with <time.h>
  5. Date: 19 Mar 96 11:58:31 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.827236711@rscernix>
  8. References: <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <Pine.A32.3.91.960318145539.118335A-100000@red.weeg.uiowa.edu> The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
  13.  
  14.  
  15. >  On a side note, what does happen if you pass an incompletely filled 
  16. >struct tm (ie, some fields set to 0) to mktime()?  I got some pretty 
  17. >colorful results from DEC C...
  18.  
  19.     4.12.2.3 The mktime function
  20.  
  21.     Synopsis
  22.  
  23.          #include <time.h>
  24.          time_t mktime(struct tm *timeptr);
  25.  
  26.     Description
  27.  
  28.     The mktime function converts the broken-down time, expressed as
  29.     local time, in the structure pointed to by timeptr into a calendar
  30.     time value with the same encoding as that of the values returned by
  31.     the time function.  The original values of the tm_wday and tm_yday
  32.     components of the structure are ignored, and the original values of
  33.     the other components are not restricted to the ranges indicated
  34.     above.  On successful completion, the values of the tm_wday and
  35.     tm_yday components of the structure are set appropriately, and the
  36.     other components are set to represent the specified calendar time, but
  37.     with their values forced to the ranges indicated above; the final
  38.     value of tm_mday is not set until tm_mon and tm_year are determined.
  39.  
  40.     Returns
  41.  
  42.     The mktime function returns the specified calendar time encoded as
  43.     a value of type time_t .  If the calendar time cannot be represented,
  44.     the function returns the value (time_t)-1 .
  45.  
  46.     Example
  47.  
  48.        What day of the week is July 4, 2001? 
  49.        
  50.  
  51.          #include <stdio.h>
  52.          #include <time.h>
  53.          static const char *const wday[] = {
  54.               "Sunday", "Monday", "Tuesday", "Wednesday",
  55.               "Thursday", "Friday", "Saturday", "-unknown-"
  56.          };
  57.          struct tm time_str;
  58.  
  59.  
  60.  
  61.          time_str.tm_year   = 2001 - 1900;
  62.          time_str.tm_mon    = 7 - 1;
  63.          time_str.tm_mday   = 4;
  64.          time_str.tm_hour   = 0;
  65.          time_str.tm_min    = 0;
  66.          time_str.tm_sec    = 1;
  67.          time_str.tm_isdst  = -1;
  68.          if (mktime(&time_str) == -1)
  69.               time_str.tm_wday = 7;
  70.          printf("%s\n", wday[time_str.tm_wday]);
  71.  
  72. So, if DEC C behaves differently, it's broken.
  73.  
  74. Dan
  75. --
  76. Dan Pop
  77. CERN, CN Division
  78. Email: danpop@mail.cern.ch 
  79. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  80.